<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8" />
    <title>智慧社区-物业管理</title>

    <meta name="description" content="Dashboard" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/resource/assets/img/favicon.png" type="image/x-icon">

    <!--Basic Styles-->
    <link href="${pageContext.request.contextPath}/resource/assets/css/bootstrap.min.css" rel="stylesheet" />
    <link href="${pageContext.request.contextPath}/resource/assets/css/font-awesome.min.css" rel="stylesheet" />

    <!--Beyond styles-->
    <link href="${pageContext.request.contextPath}/resource/assets/css/beyond.min.css" rel="stylesheet" type="text/css" />
    <link href="${pageContext.request.contextPath}/resource/assets/css/animate.min.css" rel="stylesheet" />
<script src="${pageContext.request.contextPath}/resource/assets/js/skins.min.js"></script>
</head>
<body>
<%@include file="../header.jsp" %>
	<!-- Main Container -->
    <div class="main-container container-fluid">
        <!-- Page Container -->
        <div class="page-container">
            <!-- Page Sidebar -->
            <%@include file="../nav.jsp" %><!-- /Page Sidebar -->
            <!-- Page Content -->
            <div class="page-content">
                <!-- Page Breadcrumb -->
                <div class="page-breadcrumbs">
                    <ul class="breadcrumb" style="margin-top: 8px;">
                        <li>
                            <i class="fa fa-home"></i>
                            <a href="#">首页</a>
                        </li>
                        <li><a href="#">门禁管理</a></li>
                        <li class="active">添加门禁</li>
                    </ul>
                </div><!-- /Page Breadcrumb -->
                <!-- Page Header -->
                <div class="page-header position-relative">
                    <div class="header-title" style="margin-top: 2px;">
                        <h1>
                            添加门禁
                        </h1>
                    </div><!--Header Buttons-->
                    <div class="header-buttons">
                        <a class="sidebar-toggler" href="#">
                            <i class="fa fa-arrows-h"></i>
                        </a>
                        <a class="refresh" id="refresh-toggler" href="">
                            <i class="glyphicon glyphicon-refresh"></i>
                        </a>
                        <a class="fullscreen" id="fullscreen-toggler" href="#">
                            <i class="glyphicon glyphicon-fullscreen"></i>
                        </a>
                    </div><!--Header Buttons End-->
                </div><!-- /Page Header -->
                <!-- Page Body -->
                <div class="page-body">
                	<div class="row">
	                	<div class="col-lg-6 col-sm-6 col-xs-12">
	                		<div class="widget">
                                        <div class="widget-header bordered-bottom bordered-palegreen">
                                            <span class="widget-caption">添加门禁</span>
                                        </div>
                                        <div class="widget-body">
                                           <div id="registration-form">
                                                        <form role="form">
                                                            <div class="form-title">
                                                            	门禁信息
                                                            </div>
                                                            	<div class="form-group">
                                                                        <span class="input-icon icon-right">
                                                                            <input type="text" class="form-control" placeholder="所属小区" value="${loginUser.loginVillage.name}" disabled>
                                                                            <i class="glyphicon glyphicon-globe"></i>
                                                                       </span>
                                                              	</div>
                                                            <div class="form-group">
                                                                 <select id="sRegion" style="width:100%;">
                                                                 		<option value="0"> 区/期 </option>
						                                                <c:forEach var="region" items="${regions }">
	                      													<option value="${region }"> ${region } </option>
	                      												</c:forEach>
                                            						</select>
                                                            </div>
                                                            <div class="form-group">
                                                                 <select id="sType" style="width:100%;">
                                                                 		<option value="0"> 门禁类型 </option>
                                                                 		<option value="1"> 单元门禁 </option>
                                                                 		<option value="2"> 区域门禁 </option>
                                            						</select>
                                                            </div>
                                                             <div class="form-group">
                                                                 <select id="screenSize" style="width:100%;">
                                                                 		<option value="0"> 屏幕尺寸 </option>
                                                                 		<option value="1"> 13吋 </option>
                                                                 		<option value="2"> 9吋 </option>
                                            						</select>
                                                            </div>
                                                            <div class="form-group">
                                                                 <select id="sBuilding" style="width:100%;">
                                                                  		<option value="0"> 栋 </option>
						                                                <option value="1"> 1 </option>
												                        <option value="2"> 2 </option>
												                        <option value="3"> 3 </option>
												                        <option value="4"> 4 </option>
												                        <option value="5"> 5 </option>
												                        <option value="6"> 6 </option>
												                        <option value="7"> 7 </option>
												                        <option value="8"> 8 </option>
												                        <option value="9"> 9 </option>
												                        <option value="10"> 10 </option>
												                        <option value="11"> 11 </option>
												                        <option value="12"> 12 </option>
												                        <option value="13"> 13 </option>
												                        <option value="14"> 14 </option>
												                        <option value="15"> 15 </option>
												                        <option value="16"> 16 </option>
												                        <option value="17"> 17 </option>
												                        <option value="18"> 18 </option>
												                        <option value="19"> 19 </option>
												                        <option value="20"> 20 </option>
												                        <option value="21"> 21 </option>
												                        <option value="22"> 22 </option>
												                        <option value="23"> 23 </option>
												                        <option value="24"> 24 </option>
												                        <option value="25"> 25 </option>
												                        <option value="26"> 26 </option>
												                        <option value="27"> 27 </option>
												                        <option value="28"> 28 </option>
												                        <option value="29"> 29 </option>
												                        <option value="30"> 30 </option>
                                            						</select>
                                                            </div>
                                                            <div class="form-group">
                                                                 <select id="sEntrance" style="width:100%;">
                                                                 		<option value="0"> 单元 </option>
						                                                <option value="1"> 1 </option>
												                        <option value="2"> 2 </option>
												                        <option value="3"> 3 </option>
												                        <option value="4"> 4 </option>
												                        <option value="5"> 5 </option>
												                        <option value="6"> 6 </option>
												                        <option value="7"> 7 </option>
												                        <option value="8"> 8 </option>
												                        <option value="9"> 9 </option>
												                        <option value="10"> 10 </option>
                                            						</select>
                                                            </div>
                                                            <div class="form-group">
                                                                <span class="input-icon icon-right">
                                                                    <input id="hardwareId" type="text" class="form-control"  placeholder="硬件ID" value="${um.hardwareId }">
                                                                    <i class="glyphicon glyphicon-align-right"></i>
                                                                </span>
                                                            </div>
                                                            
                                                          <div class="form-group">
                                                                        <span class="input-icon icon-right">
                                                                            <input id="number" type="text" class="form-control" placeholder="编号" value="${um.number }">
                                                                            <i class="glyphicon glyphicon-sort-by-order"></i>
                                                                        </span>
                                                                    </div>
                                                            <div class="form-group">
                                                                <span class="input-icon icon-right">
                                                                    <input id="placeDes" type="text" class="form-control" placeholder="位置描述" value="${um.placeDes }">
                                                                    <i class="glyphicon glyphicon-map-marker"></i>
                                                                </span>
                                                            </div>
                                                            <hr class="wide" />
                                                            <div class="row" style="text-align: right;margin-right: 10px;">
                                                            	<button type="button" class="btn btn-warning" onclick="cancel()"> 取 消 </button>
                                                            	<button type="button" class="btn btn-blue" onclick="save()"> 添 加 </button>
                                                            </div>
                                                            
                                                        </form>
                                                    </div> 
                                        </div>
                                    </div>
	                	</div>
                	
                    </div>
                </div><!-- /Page Body -->
             </div><!-- /Page Content -->
     	</div><!-- /Page Container -->
     </div><!-- /Main Container -->
     	<script src="${pageContext.request.contextPath}/resource/assets/js/jquery-2.0.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/resource/assets/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/resource/assets/js/select2/select2.js"></script>
    <!--Beyond Scripts-->
    <script src="${pageContext.request.contextPath}/resource/assets/js/beyond.min.js"></script>
    <script type="text/javascript">
    	$('.loading-container').hide();
    	$('#menu_um').addClass("active open");
    	$('#menu_umadd').addClass("active");
    	$("#sRegion").select2();
    	$("#sType").select2();
    	$("#sBuilding").select2();
    	$("#sEntrance").select2();
    	$("#sType").on("change",function(event){
    		if(event.val == "2" || event.val == 2){
    			$('#sBuilding').attr('disabled',true);
    			$('#sEntrance').attr('disabled',true);
    		}else{
    			$('#sBuilding').attr('disabled',false);
    			$('#sEntrance').attr('disabled',false);
    		}
    	});
    	var objectId = "${um.objectId}";
    	if(objectId != ""){
   			$('#sRegion').val("${um.region}").trigger("change");
   			$('#sType').val("${um.type}").trigger("change");
   			$('#sBuilding').val("${um.building}").trigger("change");
   			$('#sEntrance').val("${um.entrance}").trigger("change");
   		}
    	
    	function save(){
    		var type = $('#sType').val();
    		var region = $('#sRegion').val();
    		var building = $('#sBuilding').val();
    		var entrance = $('#sEntrance').val();
    		var number = $('#number').val();
    		var placeDes = $('#placeDes').val();
    		var hardwareId = $('#hardwareId').val();
    		var screenSize = $('#screenSize').val();
    		if (type == 2 || type == "2") {
    			building = "";
    			entrance = "";
    		} 
    		if (screenSize == 0) {
				alert("请选择屏幕尺寸");
				return;
			}
    		
    		if ($.trim(hardwareId) == "" || $.trim(number) == "" || $.trim(placeDes) == "") {
    			alert("请完善门禁信息后再提交保存");
    			return;
    		}
    		
    		$('.loading-container').show();
    		$.ajax({
    			"url":"${pageContext.request.contextPath}/um_save",
    			"data":{objectId:objectId,type:type,region:region,building:building,entrance:entrance,number:number,placeDes:placeDes,hardwareId:hardwareId,screenSize:screenSize},
    			"type":'post',
    			"dataType":'json',
    			"success" : function(result) {
    				$('.loading-container').hide();
    				
    				if (result == 202) {
						alert("硬件ID重复,无法添加！");
						return;					
					}
    				
    				if (result == 201){
    					alert("鉴权已过期，请重新登录");
    					location.href="/WisdomHome";
    					return;
    				}
    				
    				if (result == 200) {
    					alert("门禁单元机保存成功");
    					$('#region').val("");
    					$('#number').val("");
    					$('#hostName').val("");
    					$('#hostId').val("");
    				}else {
    					alert("提交失败，请检查网络后重试~");
    				}
    			},
    			"error" : function(msg) {
    				alert("error msg:"+msg.responseText);
    			}
    		});
    	}
    	
    	function cancel(){
    		$('#region').val("");
			$('#number').val("");
			$('#hostName').val("");
			$('#hostId').val("");
			$('#sRegion').val("0").trigger("change");
   			$('#sType').val("0").trigger("change");
   			$('#sBuilding').val("0").trigger("change");
   			$('#sEntrance').val("0").trigger("change");
    	}
    	
    	
    </script>
</body>
</html>